<?php

namespace App\Http\Controllers;

use App\Models\cms\Category;
use App\Models\cms\Content;
use App\Models\cms\ContentBlog;
use App\Models\Course;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {

//        $this->middleware('auth');

    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('home');
    }


    /**
     * @param Content $content
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getContent($id)
    {
        $content = Content::query()->where('id', $id)->first();
        if (!$content) {
            return redirect('/');
        }
        $content->view_count += 1;
        $content->save();
        $tags = explode(',', $content->tags);
        $tuijian = Content::query()
            ->where('content_detail_type', 'ContentBlog')
            ->where('id', '!=', $content->id)
            ->when($tags, function ($query) use ($tags) {
                $query->where('tags', 'like', "%{$tags[0]}%");
            })->get();
        return view('site.content', [
            'content' => $content,
            'tuijian' => $tuijian
        ]);
    }


    /**
     * @param Request $request
     * @param null $id
     * @param string $type
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getList(Request $request, $name = null)
    {

        if (!is_numeric($name)) {
            $category = Category::query()->where('name',$name)->first();
            if(!$category){
                return back();
            }
        }else{
            $category = Category::query()->where('id',$name)->first();
            if(!$category){
                return back();
            }
            $name = $category->name;
        }
        $query = Content::query()
            ->where('content_detail_type', 'ContentBlog')
            ->orderByDesc('id')
            ->whereHas('categoryLabel', function ($query) use ($category) {
                    $query->where('selected_label','like',"%-{$category->id}-%");
            });
        $content = $query->paginate(15);
        return view('site.list', [
            'name' => $name,
            'content' => $content,
        ]);
    }


    /**
     * @param Request $request
     * @param null $id
     * @param string $type
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getPictureList(Request $request, $name = null)
    {

        if (!is_numeric($name)) {
            $category = Category::query()->where('name',$name)->first();
            if(!$category){
                return back();
            }
        }else{
            $category = Category::query()->where('id',$name)->first();
            if(!$category){
                return back();
            }
            $name = $category->name;
        }
        $query = Content::query()
            ->where('content_detail_type', 'ContentBlog')
            ->orderByDesc('id')
            ->whereHas('categoryLabel', function ($query) use ($category) {
                $query->where('selected_label','like',"%-{$category->id}-%");
            });
        $content = $query->paginate(6);
        return view('site.picture', [
            'name' => $name,
            'content' => $content,
        ]);
    }
    public function teachers(){
        $courses = Course::query()->whereHas('teachers',function ($query){
            return $query->where('id','>',0);
        })->get();
        return view('site.teacher',[
            'courses'=>$courses
        ]);
    }

}
